package cn.edu.scau.cmi.yanghongyu.restcontroller;

import java.io.IOException;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.front.service.CmiFoodTypeIdService;

@Controller
public class ExternalRestController extends CmiEmaController {
	
	@Autowired
	CmiFoodTypeIdService cmiFoodTypeIdService;
	
	@RequestMapping(value = "/addressJson/{parentId}", method = RequestMethod.GET)
	@ResponseBody
	public Object loadAddresssByParentId(@PathVariable Integer parentId) {
		return JSONObject.parse(cmiPsonService.toJsonString(addressDAO.findAddresssByParent(parentId, -1, -1), 1));//不分页，直接拿所有数据。有空再完善--杨宏宇
	}
	
	@RequestMapping(value = "/restListEventsDesc", method = RequestMethod.POST)
	@ResponseBody
	public Object restListEventsDesc(@RequestBody CmiPagination cmiPagination) {
		
		return JSONObject.parse(cmiPsonService.toPsonString(cmiEventDAO.findAllEventsDesc(-1, -1), 2, cmiPagination));
	}
	
	@RequestMapping("/externalSelectBox")
	@ResponseBody
	public JSONObject eventSelectBox(@RequestParam(required = false) String searchTable,
			@RequestParam(required = false) Set<Integer> searchValue, // 杨宏宇2019-9-10：searchValue是在selectpage.js的616行定义，如果要修改searchValue的名称，1.可以修改在selectpage.js修改；2.可以
																		// @RequestParam(value="searchValue", required =
																		// false) Set<Integer> someName
			@RequestParam(required = false, defaultValue = "10") int paginationSize,
			@RequestParam(required = false, defaultValue = "1") int pageNumber,
			@RequestParam(value = "qWord[]", required = false) String queryword,
			@RequestParam(required = false) String selectItem, 
			@RequestParam(required = false) Integer foodtypeId) throws IOException {
		Set<?> entitySet = null;
		Integer count = 0;
		Set<Integer> foodtypeIdSet = null;
		
		
		if(cmiFoodTypeIdService.isAgriculturalProduct(foodtypeId)) {
			foodtypeIdSet = cmiFoodTypeIdService.getAgriculturalProductIdSet();
		} else if(cmiFoodTypeIdService.isAquatic(foodtypeId)) {
			foodtypeIdSet = cmiFoodTypeIdService.getAquaticIdSet();
		} else if(cmiFoodTypeIdService.isGrainAndOil(foodtypeId)) {
			foodtypeIdSet = cmiFoodTypeIdService.getGrainAndOilIdSet();
		} else if(cmiFoodTypeIdService.isMeat(foodtypeId)) {
			foodtypeIdSet = cmiFoodTypeIdService.getAgriculturalProductIdSet();
		} else if(cmiFoodTypeIdService.isMilk(foodtypeId)) {
			foodtypeIdSet = cmiFoodTypeIdService.getMilkIdSet();
		} else if(cmiFoodTypeIdService.isOther(foodtypeId)) {
			foodtypeIdSet = cmiFoodTypeIdService.getOtherIdSet();
		}

		// 初始化原始值
		if (searchValue != null) {
			switch (searchTable) {
			case "Event":
				entitySet = eventService.loadEventsByIdSet(searchValue);
				break;
			}
			String jsonData = cmiPsonService.toJsonString(entitySet, 1);
			JSONArray jsonArray = new JSONArray();
			jsonArray = (JSONArray) JSON.parse((String) jsonData);

			JSONObject jsonObject = new JSONObject();
			jsonObject.put("list", jsonArray);
			jsonObject.put("totalRow", entitySet.size());
			return jsonObject;
		}

		// 不根据关键字进行搜索
		if (queryword == null || queryword.trim() == "") {
			switch (selectItem) {
			case "Event":
				if(foodtypeIdSet == null) {
					entitySet = eventDAO.findAllEvents((pageNumber - 1) * paginationSize, paginationSize);
					count = eventService.countEvents();
				} else {
					entitySet = cmiEventDAO.findEventsByFoodtypeId(foodtypeIdSet, (pageNumber - 1) * paginationSize, paginationSize);
					count = cmiEventDAO.countEventsByFoodtypeId(foodtypeIdSet, -1, -1);
				}
				break;

			}
		} else {
			switch (selectItem) {
			case "Event":
				if(foodtypeIdSet == null) {
					entitySet = eventDAO.findAllEvents((pageNumber - 1) * paginationSize, paginationSize);
					count = eventService.countEvents();
				} else {
					entitySet = cmiEventDAO.findEventsByFoodtypeId(foodtypeIdSet, (pageNumber - 1) * paginationSize, paginationSize);
					count = cmiEventDAO.countEventsByFoodtypeId(foodtypeIdSet, -1, -1);
				}
				break;
			}
		}

		/**
		 * 将Json数据转换成jsonArray形式，需要通过pson方式进行转换，然后在转换成JsonArray因为存在懒加载
		 * 没有直接使用cmiPsonService中的fromJson方法是因为，fromJson方法会将int类型的直接转换成double类型的数据，这个问题还没有解决
		 */
		String jsonData = cmiPsonService.toJsonString(entitySet, 1);
		JSONArray jsonArray = new JSONArray();
		jsonArray = (JSONArray) JSON.parse((String) jsonData);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("list", jsonArray);
		jsonObject.put("paginationSize", paginationSize);
		jsonObject.put("pageNumber", pageNumber);
		jsonObject.put("totalRow", count);
		jsonObject.put("totalPaginations", count % paginationSize == 0 ? count / paginationSize : count / paginationSize + 1);
		return jsonObject;
	}
}
